<?php

class MailsAction extends BaseAction
{

    public function index()
    {
        $mail = D("Mail");
        import("ORG.Util.Page");
        $count = $mail->count();
        $Page = new Page($count, 20);
        $Page->parameter .= "keyword=" . urlencode($kmap) . "&";
        $show = $Page->show();
        $mails = $mail->order('status desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('pages', $show);
        $this->assign("mails", $mails);
        $this->assign("title", '邮件列表');
        $this->display("Public:mail");
    }

    public function send()
    {
        Vendor('PHPMailer.PHPMailer');
        $id = $_GET['id'];

        $mail             = new PHPMailer();

        $mail->IsSMTP(); 
        $mail->CharSet    = "UTF-8";
        $mail->Host       = "smtp.gmail.com"; 
        //$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                                   // 1 = errors and messages
                                                   // 2 = messages only
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
        $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
        $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
        $mail->Username   = "system@quwanma.com";  // GMAIL username
        $mail->Password   = "19831023";            // GMAIL password

        $mail->SetFrom('system@quwanma.com', 'quwanma.com');

        $mail->AddReplyTo("sunbo@quwanma.com","quwanma");
                $mails = D("Mail")->getById($id);
        $mail->Subject    = $mails['subject'];

        $mail->AltBody    = $mails['content']; // optional, comment out and test

        $mail->MsgHTML($mails['content']);

        $address = $mails['to'];
        $mail->AddAddress($address, $address);
        if (!$mail->Send()) {
            $this->error("发送失败！");
        } else {
            $data['id'] = $id;
            $data['status'] = 1;
            D("Mail")->save($data);
            $this->assign("jumpUrl", "__URL__");
            $this->success("发送成功！");
        }
    }

    public function batch()
    {
        $this->_batch();
    }

}

?>